<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Message;
use App\Model\SuperAdmin;
use App\Model\UserProfile;
use App\Model\VideoLink;
use App\Util\InfoUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class SuperAdminController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('超管员配置');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加');
            $content->description('');
            $content->body($this->form());
        });
    }

    protected function grid()
    {
        return Admin::grid(SuperAdmin::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->user_profile_id('超管员ID');
            $grid->created_at('创建时间');

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    protected function form($mode = '', $id = '')
    {
        return Admin::form(SuperAdmin::class, function (Form $form) use ($mode, $id) {
            $form->text('user_profile_id', '超管员ID')->required();

            $form->saving(function (Form $form) {
                $user = UserProfile::where('user_profile_id', $form->user_profile_id)->first();
                if (!$user) {
                    return InfoUtil::adminErr('用户信息不存在！');
                }

                $m = $form->model();
                $m->user_id = $user->user_id;
                $m->save();
            });
        });
    }
}
